//
//  SCDataViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "SCDataViewController.h"
#import "ActivitySingleton.h"
#import "SCDataTableViewCell.h"
#import "SCList.h"
#import "ActivityDetailViewController.h"
@interface SCDataViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain)NSMutableArray *myArray;



@end

@implementation SCDataViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"收藏的活动";
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    
    [[ActivitySingleton shareActivitySingleton] openAB];
    [[ActivitySingleton shareActivitySingleton] createTable];
    
    self.myArray = [[ActivitySingleton shareActivitySingleton] selectActivity:name];
//    NSLog(@">>>>>>>>%@", self.myArray);
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [_myTableView release];
    // 右侧系统自带按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    [self.myTableView reloadData];
}

// 系统编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated: animated];
    [self.myTableView setEditing:editing animated:animated];
}
// 询问哪行需要编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
// 哪行编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
// 实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
//        NSString *key = [[[ActivitySingleton shareActivitySingleton] selectActivity:name] objectAtIndex:indexPath.section];
//        NSMutableArray *array = [[ActivitySingleton shareActivitySingleton] selectActivity:name];
        //删除数据
        Activity *act = [self.myArray objectAtIndex:indexPath.row];
        NSLog(@"act%@", act.name);
        [[ActivitySingleton shareActivitySingleton] deleteActivity:act.name];
        // 删除对象
        [self.myArray removeObjectAtIndex:indexPath.row];
        // 删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationBottom];
     
    }
}





// 和cell有关部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    SCDataTableViewCell *cell = [[SCDataTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    SCList *scl = [self.myArray objectAtIndex:indexPath.row];
    cell.shoucang = scl;
    return cell;
}

// 点击触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailViewController *activityDetaVC = [[ActivityDetailViewController alloc] init];
    
    Activity *activity = [self.myArray objectAtIndex:indexPath.row];
    //传整个activity
    activityDetaVC.activiyX = activity;
    [self.navigationController pushViewController:activityDetaVC animated:YES];
    
    [activityDetaVC release];
   
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
